<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="x-ua-compatible" content="IE=edge">
  <title>Swagger UI</title>
  <link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32" />
  <link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16" />
  <link href='css/typography.css' media='screen' rel='stylesheet' type='text/css'/>
  <link href='css/reset.css' media='screen' rel='stylesheet' type='text/css'/>
  <link href='css/screen.css' media='screen' rel='stylesheet' type='text/css'/>
  <link href='css/reset.css' media='print' rel='stylesheet' type='text/css'/>
  <link href='css/print.css' media='print' rel='stylesheet' type='text/css'/>

  <script src='lib/object-assign-pollyfill.js' type='text/javascript'></script>
  <script src='lib/jquery-1.8.0.min.js' type='text/javascript'></script>
  <script src='lib/jquery.slideto.min.js' type='text/javascript'></script>
  <script src='lib/jquery.wiggle.min.js' type='text/javascript'></script>
  <script src='lib/jquery.ba-bbq.min.js' type='text/javascript'></script>
  <script src='lib/handlebars-4.0.5.js' type='text/javascript'></script>
  <script src='lib/lodash.min.js' type='text/javascript'></script>
  <script src='lib/backbone-min.js' type='text/javascript'></script>
  <script src='swagger-ui.js' type='text/javascript'></script>
  <script src='lib/highlight.9.1.0.pack.js' type='text/javascript'></script>
  <script src='lib/highlight.9.1.0.pack_extended.js' type='text/javascript'></script>
  <script src='lib/jsoneditor.min.js' type='text/javascript'></script>
  <script src='lib/marked.js' type='text/javascript'></script>
  <script src='lib/swagger-oauth.js' type='text/javascript'></script>

  <!-- Some basic translations -->
  <!-- <script src='lang/translator.js' type='text/javascript'></script> -->
  <!-- <script src='lang/ru.js' type='text/javascript'></script> -->
  <!-- <script src='lang/en.js' type='text/javascript'></script> -->

<style>

</style>
  <script type="text/javascript">
    var api_root = "/api";
    $(function () {
    	/*
      var url = window.location.search.match(/url=([^&]+)/);
      if (url && url.length > 1) {
        url = decodeURIComponent(url[1]);
      } else {
        url = "http://petstore.swagger.io/v2/swagger.json";
      }
      */
      var url = location.origin + "/api-docs";
      hljs.configure({
        highlightSizeThreshold: 5000
      });

      // Pre load translate...
      if(window.SwaggerTranslator) {
        window.SwaggerTranslator.translate();
      }
      window.swaggerUi = new SwaggerUi({
        url: url,
        validatorUrl:undefined,  // Disable json validation from external website
        dom_id: "swagger-ui-container",
        apisSorter: "alpha",
        supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
        onComplete: function(swaggerApi, swaggerUi){

          if(typeof initOAuth == "function") {
            initOAuth({
              clientId: "your-client-id",
              clientSecret: "your-client-secret-if-required",
              realm: "your-realms",
              appName: "your-app-name",
              scopeSeparator: " ",
              additionalQueryStringParams: {}
            });
          }

          if(window.SwaggerTranslator) {
            window.SwaggerTranslator.translate();
          }
        },
        onFailure: function(data) {
          log("Unable to Load SwaggerUI");
        },
        docExpansion: "none",
        jsonEditor: false,
        defaultModelRendering: 'schema',
        showRequestHeaders: false,
        showOperationIds: false
      });

      window.swaggerUi.load();

      function log() {
        if ('console' in window) {
          console.log.apply(console, arguments);

        }
      }
  });

    function login(){
    	var userName = document.getElementById("sonic_username").value;
    	var pwd = document.getElementById("sonic_password").value;

    	var authEndPoint = swaggerUi.options.url.substring(0,swaggerUi.options.url.indexOf(api_root)) + "/session";
    	var xhr = new XMLHttpRequest();
    	xhr.onreadystatechange = function() {
       	    if (xhr.readyState == 4) {
       	    	if (xhr.status == 200){
	       	    	var jsonResp = JSON.parse(this.responseText);
	       	    	if (jsonResp.operationStatus==="SUCCESS"){
		       	    	swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("Authorization", jsonResp.item.token, "header"));
		       	    	localStorage.setItem("swaggerjwt",jsonResp.item.token);
		       	    	alert ("Authenticated !!!, Welcome " + jsonResp.item.firstName);
	       	    	}
	       	    	else{
	       	    		alert ("ERROR: Unable to Authenticate");
	       	    	}
       	    	}
       	    	else{
       	    		alert ("ERROR: " + xhr.responseText);
       	    	}
       	    }
       	};
       	xhr.open("POST", authEndPoint, true);
       	xhr.setRequestHeader('Content-Type', 'application/json');
    	xhr.setRequestHeader('Accept', 'application/json');
    	jsonStr = '{"username":"'+ userName +'", "password":"'+ pwd +'"}';
    	console.log(jsonStr);
       	xhr.send(jsonStr);
    }

    function restAuthenticate(swaggerUi){
    }



    </script>
</head>

<body class="swagger-section">
<div id='header'>
  <div class="swagger-ui-wrap">
    <form id='api_selector'>
      <div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text" style="width:330px"/></div>
      <div id='auth_container'></div>
      <div class='input'><a id="explore" class="header__btn" href="#" data-sw-translate>Explore</a></div>
      <!-- START Added by MRIN -->
      <input placeholder="User Name" id="sonic_username" name="username" type="text" value="demo"    style="width:75px;vertical-align: middle;"/>
      <input placeholder="Password" id="sonic_password"  name="password" type="password" value="demo" style="width:75px;vertical-align: middle;"/>
      <button id="loginBtn" type="button" class="header__btn"  style="border:0; color:white" onclick="login()">Login</button>
      <!-- END Added by Mrin -->
    </form>
  </div>
</div>

<div id="message-bar" class="swagger-ui-wrap" data-sw-translate>&nbsp;</div>
<div id="swagger-ui-container" class="swagger-ui-wrap"></div>
</body>
</html>
